<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/8/27
 * Time: 11:28
 * Comment: ''
 */

namespace App\Repository;

use App\Models\MemberExtend;
use App\Models\User;

class MemberRepo extends BaseRepo
{
    /**
     * @param $address
     * @return int
     */
    public function getIdByAddress($address): int
    {
        $user = User::query()->where("ca_account_address", $address)->first(["id"]);
        return $user ? (int)$user->id : 0;
    }

    /**
     * 根据手机号获取信息
     * @param string $phone
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     * @author shen.zk
     * @date 2023-08-27 11:29
     */
    public function getByPhoneRepo(string $phone)
    {
        return User::query()->where('mobile', $phone)->first();
    }

    /**
     * 写入用户数据
     * @param array $data
     * @return int
     * @author shen.zk
     * @date 2023-08-27 12:06
     */
    public function addMemberInfoRepo(array $data, int $id = 0)
    {
        if ($id) {
            return User::query()->where('id', $id)->update($data);
        } else {
            return User::query()->insertGetId($data);
        }

    }

    /**
     * 写入扩展信息
     * @param array $data
     * @param int $member_id
     * @return int
     * @author shen.zk
     * @date 2023-09-04 15:01
     */
    public function memberExtendInfoRepo(array $data, int $member_id = 0)
    {
        if ($member_id) {
            return MemberExtend::query()->where('member_id', $member_id)->update($data);
        } else {
            return MemberExtend::query()->insertGetId($data);
        }
    }

    /**
     * 根据用户ID获取扩展信息
     * @param int $member_id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-08-28 16:50
     */
    public function getByMemrberId(int $member_id)
    {
        return MemberExtend::query()->where('member_id', $member_id)->lock(true)->first();
    }

    /**
     * 根据手机号查询信息
     * @param string $mobile
     * @return array
     * Author:xsc
     * Time:2023/9/5 14:55
     */
    public function getInfoByMobile(string $mobile)
    {
        $fields = [
            'm.id as member_id', 'm.nickname', 'm.portrait', 'm.ca_account_address',
            'e.member_id', 'e.real_name_status', 'e.real_time',
        ];
        $res = MemberExtend::query()
            ->from('member_extend', 'e')
            ->join('member as m', 'e.member_id', '=', 'm.id')
            ->where('m.mobile', $mobile)
            ->first($fields);
        return $res ? $res->toArray() : [];
    }

    /**
     * 根据id查询信息
     * @param int $member_id
     * @return array
     * Author:xsc
     * Time:2023/10/13 17:28
     */
    public function getInfoById(int $member_id)
    {
        $fields = [
            'm.id as member_id', 'm.mobile', 'm.nickname', 'm.portrait', 'm.ca_account_address',
            'e.member_id', 'e.real_name_status', 'e.real_time',
        ];
        $res = MemberExtend::query()
            ->from('member_extend', 'e')
            ->join('member as m', 'e.member_id', '=', 'm.id')
            ->where('m.id', $member_id)
            ->first($fields);
        return $res ? $res->toArray() : [];
    }

}
